package com.diet263.activity;

import java.io.UnsupportedEncodingException;

import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.diet263.nfcleedemo.R;
import com.diet263.utils.MyLog;

public class NFCMainActivity extends BaseActivity implements OnClickListener {
	private NfcAdapter mNfcAdapter;
	private PendingIntent mPendingIntent;
	private String readResult = "";
	private TextView tvTagNDEF;
	private TextView tvTagInfo;
	private Button btnRunApplication;
	private String TAG = "NFCMainActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_scrollview);
		initView();
	}

	private void initView() {
		mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
		mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0);
		tvTagNDEF = (TextView) findViewById(R.id.tv_nfc_tag_ndef);
		tvTagInfo = (TextView) findViewById(R.id.tv_nfc_tag_info);
		btnRunApplication = (Button)findViewById(R.id.btn_control);
		btnRunApplication.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (mNfcAdapter != null)
			mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())
				|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {

			if (readFromTag(intent)) {
				tvTagNDEF.setText(readResult);
			} else {
				tvTagNDEF.setText("标签数据为空");
			}
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (mNfcAdapter != null)
			mNfcAdapter.disableForegroundDispatch(this);
	}

	private boolean readFromTag(Intent intent) {
		Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
		if (detectedTag == null) {
			MyLog.i(TAG, "detectedTag == null ");
			return false;
		} else {
			byte[] Ids = detectedTag.getId();
			MyLog.i(TAG, "detectedTag.getId() = " + new String(Ids));
			String[] techList = detectedTag.getTechList();
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < techList.length; i++) {
				stringBuilder.append(techList[i]).append("\n");
			}
			tvTagInfo.setText(stringBuilder.toString());
		}
		Parcelable[] rawArray = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
		if (rawArray != null) {
			NdefMessage mNdefMsg = (NdefMessage) rawArray[0];
			NdefRecord mNdefRecord = mNdefMsg.getRecords()[0];
			try {
				if (mNdefRecord != null) {
					readResult = new String(mNdefRecord.getPayload(), "UTF-8");
					return true;
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			return false;
		}
		return false;
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_control:
			startActivity(new Intent(this, ControlActivity.class));
			break;

		default:
			break;
		}
	}

}
